package com.ctshk.app.user.controller;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.system.dto.AreaHasChildrenDTO;
import com.ctshk.rpc.system.dto.MainDataRowFieldDataDTO;
import com.ctshk.rpc.system.service.ISysAreaService;
import com.ctshk.rpc.user.dto.PassengerDTO;
import com.ctshk.rpc.user.dto.PassengerListDTO;
import com.ctshk.rpc.user.req.BatchDeleteReq;
import com.ctshk.rpc.user.req.PassengerPageReq;
import com.ctshk.rpc.user.req.PassengerReq;
import com.ctshk.rpc.user.service.IPassengerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 *  常用旅客前端控制器
 * </p>
 *
 * @author 李新科
 * @date 2021/5/17 10:22
 */
@Api(value="常用旅客", tags="常用旅客管理")
@RestController
@RequestMapping("/passenger")
public class PassengerController extends AppBaseController {

    @DubboReference
    private IPassengerService passengerService;

    @DubboReference
    private ISysAreaService sysAreaService;

    @ApiOperation(value="常用旅客列表", notes="常用旅客列表")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.QUERY, operTitle = "常用旅客列表", operDesc = "当前用户下的常用旅客列表")
    @PostMapping("/list")
    public Result<PageResponse<PassengerListDTO>> queryPage(@RequestBody @Valid PassengerPageReq req) throws Exception{
        TokenUser currentUser = getCurrentUser();
        req.setUserId(currentUser.getId());
        return passengerService.queryPage(req);
    }

    @ApiOperation(value="新增常用旅客", notes="新增常用旅客")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.ADD, operTitle = "新增常用旅客", operDesc = "新增常用旅客")
    @PostMapping("/add")
    public Result add(@RequestBody @Valid PassengerReq req) throws Exception{
        TokenUser currentUser = getCurrentUser();
        req.setUserId(currentUser.getId());
        return passengerService.add(req);
    }

    @ApiOperation(value="修改常用旅客", notes="修改常用旅客")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.UPDATE, operTitle = "修改常用旅客", operDesc = "修改常用旅客")
    @PostMapping("/update")
    public Result update(@RequestBody @Valid PassengerReq req) throws Exception{
        TokenUser currentUser = getCurrentUser();
        req.setUserId(currentUser.getId());
        return passengerService.update(req);
    }

    @ApiOperation(value="删除常用旅客", notes="删除常用旅客")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.DELETE, operTitle = "删除常用旅客", operDesc = "删除常用旅客")
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable @NotNull(message = "id不能为空")
                             @ApiParam(name="id",required = true) Long id) throws Exception{
        TokenUser currentUser = getCurrentUser();
        return passengerService.delete(id,currentUser.getId());
    }

    @ApiOperation(value="批量删除常用旅客", notes="批量删除常用旅客")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.DELETE, operTitle = "批量删除常用旅客", operDesc = "批量删除常用旅客")
    @PostMapping("/batchDelete")
    public Result batchDelete(@RequestBody BatchDeleteReq req) throws Exception{
        TokenUser currentUser = getCurrentUser();
        req.setUserId(currentUser.getId());
        return passengerService.batchDelete(req);
    }

    @ApiOperation(value="常用旅客详情", notes="常用旅客详情")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.QUERY, operTitle = "常用旅客详情", operDesc = "常用旅客详情")
    @PostMapping("/detail/{id}")
    public Result<PassengerDTO> detail(@PathVariable @NotNull(message = "id不能为空")
                         @ApiParam(name="id",required = true) Long id) throws Exception{
        TokenUser currentUser = getCurrentUser();
        return passengerService.detail(id,currentUser.getId());
    }

    @ApiOperation(value="获取国家/地区类型数据", notes="获取国家/地区类型数据")
    @GetMapping(value = "/getAreaData")
    public Result<List<AreaHasChildrenDTO>> getAreaData() throws Exception{
        List<AreaHasChildrenDTO> areaHasChildrenDTOS = sysAreaService.queryFirstArea();
        return Result.success(areaHasChildrenDTOS);
    }

    @ApiOperation(value="设为默认旅客", notes="设为默认旅客")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.UPDATE, operTitle = "设为默认旅客", operDesc = "设为默认旅客")
    @PostMapping("/setDefault/{id}/{isDefault}")
    public Result setDefaultAddress(@PathVariable @NotNull(message = "id不能为空")
                                    @ApiParam(name="id",required = true) Long id,
                                    @PathVariable
                                    @Min(value = 0,message = "非法参数")
                                    @Max(value = 1,message = "非法参数")
                                    @ApiParam(name="是否默认,0否1是",required = true) Integer isDefault) throws Exception{
        TokenUser currentUser = getCurrentUser();
        return passengerService.setDefault(id,currentUser.getId(),isDefault);
    }
}
